package com.spzx.product;

import com.spzx.common.security.annotation.EnableCustomConfig;
import com.spzx.common.security.annotation.EnableRyFeignClients;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.mapper.ProductSkuMapper;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication
public class SpzxProductApplication implements CommandLineRunner{

    public static void main(String[] args) {
        SpringApplication.run(SpzxProductApplication.class,args);
    }

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    //商品功能
    //初始化布隆过滤器
    @Override
    public void run(String... args) throws Exception {
        String key = "sku:product:data";

        //1 查询所有skuid值
        List<ProductSku> productSkuList = productSkuMapper.selectList(null);

        //2 所有skuid放到redis的bitmap
        productSkuList.forEach(productSku -> {
            Long skuId = productSku.getId();
            redisTemplate.opsForValue().setBit(key,skuId,true);
        });
    }
}
